'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 定时任务
 * @author sundbcn@gmail.com (sun debing)
 */

var schedule = require('pomelo-schedule');
var moment = require('moment');
var cronBossClear = require('./schedule/cronBossClear');
var refreshFightPower = require('./schedule/refreshFightPower.js');
var marqueeSend = require('./schedule/marqueeSend.js');
var constantUtil = require('../util/constantUtil.js');
// var honorRankReward = require('./schedule/honorRankReward.js');
var arenaRankReward = require('./schedule/arenaRankReward.js');
var onlineUserRecord = require('./schedule/onlineUserRecord.js');
var bossRankReward = require('./schedule/bossRankReward.js');
var unionBossReset = require('./schedule/unionBossReset.js');

/*
触发时间参数
s m h d m e
| | | | | |
| | | | | +----- day of week (0 - 6) (Sunday=0)
| | | | +------- month (1 - 12)
| | | +--------- day of month (1 - 31)
| | +----------- hour (0 - 23)
| +------------- min (0 - 59)
+--------------- second (0 - 59)
*/

// 周一零点重置联盟BOSS状态(并发放当前BOSS伤害排名奖励)
schedule.scheduleJob(
    '0 0 0 * * 1',
    unionBossReset, {
        name: 'unionBossReset'
    }
);

// 刷新战力
schedule.scheduleJob(
    '0 50 4 * * *',
    refreshFightPower, {
        name: 'refreshFightPower'
    }
);

var _time = function () {
    var str = '0';
    var step = parseInt(constantUtil.value('advert_fresh_time'));
    for (var i = step; i < 60; i += step) {
        str += ',' + i.toString();
    }
    return str + ' * * * * *';
};

// 走马灯定时发送公告（按优先级别）
schedule.scheduleJob(
    _time(),
    marqueeSend, {
        name: 'marqueeSend'
    }
);

// // 周日晚上9点，发放荣誉排名奖励
// schedule.scheduleJob(
//     '0 0 21 * * 0',
//     honorRankReward, {
//         name: 'honorRankReward'
//     }
// );

// 每天晚上9点，发放竞技场排名奖励
schedule.scheduleJob(
    '0 0 21 * * *',
    arenaRankReward, {
        name: 'arenaRankReward'
    }
);

// 刷新在线人数(10分钟一次，一小时取一次平均值)
schedule.scheduleJob(
    '0 * * * * *',
    onlineUserRecord, {
        name: 'onlineUserRecord'
    }
);

// 每日清除世界boss数据的
schedule.scheduleJob(
    '0 30 19 * * *',
    cronBossClear, {
        name: 'cronBossClear'
    }
);

// 世界BOSS排行奖励发放时间(世界BOSS战结束+5分钟)
var _getBossRewardTime = function () {
    var boss_start_time = parseInt(constantUtil.value('boss_start_time'));
    var boss_end_time = parseInt(constantUtil.value('boss_end_time')) + 300;
    boss_end_time += boss_start_time;
    var hh = Math.floor(boss_end_time / 3600);
    var mm = Math.floor(boss_end_time % 3600 / 60);
    var ss = boss_end_time % 60;
    return '' + ss + ' ' + mm + ' ' + hh + ' * * *';
};

// 发放世界BOSS排行奖励
schedule.scheduleJob(
    _getBossRewardTime(),
    bossRankReward, {
        name: 'bossRankReward'
    }
);
